//
//  SortViewController.m
//  FruitVIP
//
//  Created by 孙艳玲 on 15/6/30.
//  Copyright (c) 2015年 孙艳玲. All rights reserved.
//

#import "SortViewController.h"

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

@interface SortViewController ()

@end

@implementation SortViewController


- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor  whiteColor];
    self.navigationItem.title = @"分类";
    //     [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:241.0 / 255.0 green:204.0 / 255.0 blue:48.0 / 255.0 alpha:1]}];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:110.0 / 255.0 green:78.0 / 255.0 blue:71.0 / 255.0 alpha:1]}];
    //    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:255.0/255.0 green:206.0 / 255.0 blue:0 / 255.0 alpha:1];
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
       
    UIImageView *backImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    UIImage *image = [UIImage imageNamed:@"back.png"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    backImage.contentMode = UIViewContentModeScaleAspectFit;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 5, 10, 20);
    //    button.backgroundColor = [UIColor redColor];
    [button setBackgroundImage:image forState:UIControlStateNormal];
    [button addTarget:self action:@selector(leftButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [backImage addSubview:button];
    UITapGestureRecognizer *backTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(leftButtonAction)];
    [backImage addGestureRecognizer:backTap];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backImage];
    
    
    
    //苹果推荐的使用的一个标准的瀑布流类
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //设置item的大小(方块的大小)
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH/2,50);
    //行间距(最小行间距)
    flowLayout.minimumLineSpacing = 0;
    //最小列间距
    flowLayout.minimumInteritemSpacing = 0;
    //滑动方向设置为左右滑动(默认上下滑动)
    //    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //当你是垂直滑动的时候,头部区域的大小由高来决定
//    //当你是垂直水平的时候,头部区域的大小由宽来决定
//    flowLayout.headerReferenceSize = CGSizeMake(100, 10);
//    //设置底部区域的大小
//    flowLayout.footerReferenceSize = CGSizeMake(200, 200);
    //设置item上左下右的边距大小
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    //集合视图
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:flowLayout];
    collectionView.backgroundColor = [UIColor whiteColor];
    //指定代理人
    collectionView.delegate= self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    
    
#warning 重点  注册cell  如果是自定义cell,这里必须要用自定义cell的类注册
    
    [collectionView registerClass:[SortCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];

}


- (void)leftButtonAction
{
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = NO;
}


//必须实现的两个协议方法
//设置item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 18;
}
//显示item内容的方法
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //直接从重用池里取
    //参数1.重用标示符
    //参数2.cell的位置
    SortCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
//    cell.contentView.backgroundColor = [UIColor redColor];
//    cell.contentView.backgroundColor = [UIColor colorWithRed:((10 * indexPath.row) / 255.0) green:((20 * indexPath.row)/255.0) blue:((30 * indexPath.row)/255.0) alpha:0.8f];
    
    if (indexPath.row % 4 == 0 || indexPath.row % 4 == 3) {
        cell.contentView.backgroundColor = [UIColor colorWithRed:248.0/255.0 green:248.0/255.0 blue:248.0/255.0 alpha:1];
    }
    else
      
            cell.contentView.backgroundColor = [UIColor whiteColor];

    
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *str = [NSString stringWithFormat:@"section = %ld, row = %ld", indexPath.section, indexPath.row];
    NSLog(@"str = %@", str);
    SortListViewController *sortVC = [[SortListViewController alloc] init];
    [self.navigationController pushViewController:sortVC animated:YES];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
